﻿using System;
using System.Data;

namespace DAL
{
    public static class DalOrderBuy
    {
        private static readonly clsConnect Cls = new clsConnect();
        #region[Get-Data]
        public static DataTable GetOrderBuy(string keywords)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            return Cls.GetData("sp_Order_Buy");
        }
        public static DataTable GetOrderBuyDetailEdit(int orderBuyId, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Buy_Id", orderBuyId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Order_Buy_Edit");
        }
        public static DataTable GetOrderBuyDetailSum(int orderBuyId, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Buy_Id", orderBuyId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Order_Buy_GetSum");
        }
        public static DataTable GetOrderBuyDetailSumTemp(int orderId, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", orderId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Order_Buy_GetSum");
        }
        public static DataTable GetOrderBuyDetailEditTmep(int flag, int usersId)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", usersId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Order_Buy_Edit_Temp");
        }
        public static DataTable GetOrderBuyDetailEditTmepSum(int flag, int usersId)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", usersId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Order_Buy_Detail_GetSumTemp");
        }
        public static DataTable GetOrderBuyActive(string keywords, int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            Cls.AddParameter("@IsActive", id);
            return Cls.GetData("sp_Order_Buy_Fillter");
        }
        public static DataTable GetOrderBuyFillterDate(string dateBegin, string dateEnd)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@DateBegin", dateBegin);
            Cls.AddParameter("@EndBegin", dateEnd);
            return Cls.GetData("sp_tb_Order_Buy_FillterDate");
        }
        public static DataTable GetOrder_IsUsed(string keywords, int isUsed, int isActive)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            Cls.AddParameter("@IsUsed", isUsed);
            Cls.AddParameter("@IsActive", isActive);
            return Cls.GetData("sp_Order_GetOrder_IsUsed");
        }
        public static bool GetOrderCheck(EtOrderBuyDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Buy_Id", obj.Order_Buy_Id);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Check_Proposed", "@Msg");
            return true;
        }
        #endregion

        #region[Insert-Update-Delete]
        public static bool Insert(EtOrderBuy obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", obj.Order_Id);
            Cls.AddParameter("@Order_Buy_Code", obj.Order_Buy_Code);
            Cls.AddParameter("@Order_Buy_DateBegin", obj.Order_Buy_DateBegin);
            Cls.AddParameter("@Order_Buy_DateEnd", obj.Order_Buy_DateEnd);
            Cls.AddParameter("@Order_Buy_Tax_Code", obj.Order_Buy_Tax_Code);
            Cls.AddParameter("@Order_Buy_Description", obj.Order_Buy_Description);
            Cls.AddParameter("@Order_Buy_Payment_Type", obj.Order_Buy_Payment_Type);
            Cls.AddParameter("@Order_Buy_Uesr_ID", obj.Order_Buy_Uesr_ID);
            Cls.AddParameter("@Customer_ID", obj.Customer_ID);
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            return Cls.ExecuteNonQuery("sp_Order_Buy_Insert");
        }
        public static bool Update(EtOrderBuy obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Buy_Id", obj.Order_Buy_Id);
            Cls.AddParameter("@Order_Buy_Tax_Code", obj.Order_Buy_Tax_Code);
            Cls.AddParameter("@Order_Buy_Description", obj.Order_Buy_Description);
            return Cls.ExecuteNonQuery("sp_Order_Buy_Update");
        }
        public static bool Delete(EtOrderBuy obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Buy_Id", obj.Order_Buy_Id);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Order_Buy_Delete", "@Msg");
            return true;
        }
        public static bool InsertDetail(EtOrderBuyDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Buy_Id", obj.Order_Buy_Id);
            Cls.AddParameter("@Materials_Id", obj.Materials_Id);
            Cls.AddParameter("@Order_Detail_Quality", obj.Order_Detail_Quality);
            Cls.AddParameter("@Order_Detail_Price", obj.Order_Detail_Price);
            Cls.AddParameter("@Order_Detail_CK_Percent", obj.Order_Detail_CK_Percent);
            Cls.AddParameter("@Order_Detail_CK", obj.Order_Detail_CK);
            Cls.AddParameter("@Order_Detail_VAT_Percent", obj.Order_Detail_VAT_Percent);
            Cls.AddParameter("@Order_Detail_VAT", obj.Order_Detail_VAT);
            Cls.AddParameter("@Order_Detail_Total_Price", obj.Order_Detail_Total_Price);
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            Cls.AddParameter("@Flag", obj.Flag);
            return Cls.ExecuteNonQuery("sp_Order_Buy_Detail_Insert");
        }
        public static bool InsertDetailOrder(EtOrderBuyDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", obj.Order_Id);
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            Cls.AddParameter("@Flag", obj.Flag);
            return Cls.ExecuteNonQuery("sp_tb_Order_Detail_Insert_Order");
        }
        public static bool DeleteDetail(EtOrderBuyDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Buy_Detail_Id", obj.Order_Detail_Id);
            Cls.AddParameter("@Flag", obj.Flag);
            return Cls.ExecuteNonQuery("sp_Order_Buy_Detail_Delete_Temp");
        }
        public static bool UpdateQuanlity(EtOrderBuyDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Buy_Id", obj.Order_Buy_Id);
            Cls.AddParameter("@Order_Buy_Detail_Id", obj.Order_Detail_Id);
            Cls.AddParameter("@Order_Detail_Quality", obj.Order_Detail_Quality);
            Cls.AddParameter("@Order_Detail_Price", obj.Order_Detail_Price);
            Cls.AddParameter("@Order_Detail_CK_Percent", obj.Order_Detail_CK_Percent);
            Cls.AddParameter("@Order_Detail_VAT_Percent", obj.Order_Detail_VAT_Percent);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Order_Order_Detail_Update_Quanlity", "@Msg");
            return true;
        }
        public static bool DeleteAdd(EtOrderBuyDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            Cls.AddParameter("@Flag", obj.Flag);
            return Cls.ExecuteNonQuery("sp_Order_Buy_Detail_Delete_Temp_Add");
        }
        #endregion
    }
    public class EtOrderBuy
    {
        public int ? Users_Id { get; set; }
        public bool IsActive { get; set; }
        public int ? Customer_ID { get; set; }
        public int ? Order_Buy_Uesr_ID { get; set; }
        public string Order_Buy_Payment_Type { get; set; }
        public string Order_Buy_Description { get; set; }
        public string Order_Buy_Tax_Code { get; set; }
        public DateTime Order_Buy_DateEnd { get; set; }
        public DateTime Order_Buy_DateBegin { get; set; }
        public string Order_Buy_Code { get; set; }
        public int ? Order_Id { get; set; }
        public int ? Order_Buy_Id { get; set; }
        public int Msg { get; set; }
    }
    public class EtOrderBuyDetail
    {
        public int Users_Id { get; set; }
        public float Order_Detail_VAT_Percent { get; set; }
        public float Order_Detail_VAT { get; set; }
        public float Order_Detail_CK { get; set; }
        public float Order_Detail_CK_Percent { get; set; }
        public float Order_Detail_Note { get; set; }
        public float Order_Detail_Total_Price { get; set; }
        public float Order_Detail_Price { get; set; }
        public int Order_Detail_Quality { get; set; }
        public int Materials_Id { get; set; }
        public int Order_Detail_Id { get; set; }
        public int? Order_Buy_Id { get; set; }
        public bool IsActive { get; set; }
        public float Order_Detail_Quality_Remain { get; set; }
        public int Flag { get; set; }
        public int Msg { get; set; }
        public int Order_Id { get; set; }
    }
}